package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.user.dto.UserQueryDTO;
import com.alibaba.otter.manager.web.dto.UserDTO;
import com.alibaba.otter.manager.biz.user.UserService;
import com.alibaba.otter.manager.web.common.WebConstant;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.dto.group.UserGroup;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.user.User;
import com.alibaba.otter.shared.common.utils.SecurityUtils;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.naming.AuthenticationException;
import java.util.List;

/*
 * 用户管理
 */
@RequestMapping("/api/v1/user")
@RestController
public class UserResource {


    private final UserService userService;

    public UserResource(UserService userService) {
        this.userService = userService;
    }


    /**
     * 查找用户
     */
    @GetMapping("list")
    public RestResponse<List<User>> list(UserQueryDTO userQueryDTO, Pageable pageable){
        return PaginationUtil.pageHttpResponse(userService.userPage(userQueryDTO,pageable));
    }


    /**
     * 添加用户
     */
    @PostMapping("addUser")
    public  RestResponse<Void> add(@RequestBody @Validated(UserGroup.Add.class) UserDTO userDTO){

        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        userService.createUser(user);
        return RestResponse.ok();
    }

    /**
     * 编辑用户
     */
    @PostMapping("editUser")
    public  RestResponse<Void> edit(@RequestBody @Validated(UserGroup.Edit.class) UserDTO userDTO){

        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        userService.updateUser(user);
        return RestResponse.ok();
    }

    /**
     * 登录
     */
    @PostMapping("login")
    public RestResponse<Long> login(@RequestBody @Validated(UserGroup.Login.class) UserDTO userDTO, HttpSession session) throws AuthenticationException {

        User user = userService.login(userDTO.getName(), SecurityUtils.getPassword(userDTO.getPassword()));
        if (user == null) {
            throw new AuthenticationException("登录用户不存在");
        }
        // 在session中创建User对象
        session.setAttribute(WebConstant.USER_SESSION_KEY, user);
        session.setMaxInactiveInterval(1800);
        return RestResponse.ok(user.getId());
    }

    /**
     * 登录
     */
    @GetMapping("/currUser")
    public RestResponse<User> currUser(HttpSession session) {
        User user = (User) session.getAttribute(WebConstant.USER_SESSION_KEY);
        // 在session中创建User对象
        return RestResponse.ok(user);
    }


    /**
     * 登录
     */
    @PostMapping("/info/{id}")
    public RestResponse<User> info(@PathVariable("id") Long id) throws AuthenticationException {

        User user = userService.findUserById(id);
        if (user == null) {
            throw new AuthenticationException("登录用户不存在");
        }
        // 在session中创建User对象
        return RestResponse.ok(user);
    }


    /**
     * 删除用户
     */
    @DeleteMapping("/delete/{userId}")
    public RestResponse<Void> delete(@PathVariable("userId") Long userId){
        userService.deleteUser(userId);
        return RestResponse.ok();
    }

    /**
     * 退出登录
     */
    @GetMapping("logout")
    public RestResponse<Void> logout(HttpSession session){
        session.removeAttribute(WebConstant.USER_SESSION_KEY);
        return RestResponse.ok();
    }


}
